/*******************************************************************************
* Copyright (C) 2019 China Micro Semiconductor Limited Company. All Rights Reserved.
*
* This software is owned and published by:
* CMS LLC, No 2609-10, Taurus Plaza, TaoyuanRoad, NanshanDistrict, Shenzhen, China.
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with CMS
* components. This software is licensed by CMS to be adapted only
* for use in systems utilizing CMS components. CMS shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. CMS is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/

#ifndef __MOTOR_CONTROL_H__
#define __MOTOR_CONTROL_H__
/*****************************************************************************/
/* Include files */
/*****************************************************************************/

/*CHECK_MOS*/
#define	DIR_LABC_ucChkDirTB      3
#define	DIR_HABC_ucChkDirTB      4
#define	DIR_LA_ucChkDirTB          5
#define	DIR_LB_ucChkDirTB          6
#define	DIR_LC_ucChkDirTB          7





/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define') */
/*****************************************************************************/

#define	AH_PWM									(0X3E00ul)	//0011 1110			
#define	BH_PWM									(0x3b00ul)	//0011 1011			
#define	CH_PWM									(0x2F00ul)  //0010 1111
#define	AL_PWM									(0X3D00ul)	//0011 1101			
#define	BL_PWM									(0x3700ul)	//0011 0111			
#define	CL_PWM									(0x1F00ul)  //0001 1111
#define	ABH_PWM									(0X3A00ul)	//0011 1010			
#define	ACH_PWM									(0x2E00ul)	//0010 1110			
#define	BCH_PWM									(0x2B00ul)  //0010 1011
#define	AH										(0x0001ul)  //0000 0001		
#define	BH										(0x0004ul)	//0000 0100			
#define	CH										(0x0010ul)  //0001 0000
#define	AL										(0x0002ul) 	//0000 0010			
#define	BL										(0x0008ul)  //0000 1000		
#define	CL										(0x0020ul)	//0010 0000


#if(PWM_MODE == PWM_MODE_INDPENT)
/*COMMUTATION USE*/
#define	DRIVE_A_BC								(AH_PWM | BL | CL)
#define	DRIVE_AB_C								(ABH_PWM | CL)
#define	DRIVE_B_CA								(BH_PWM | CL | AL)
#define	DRIVE_BC_A								(BCH_PWM | AL)
#define	DRIVE_C_AB								(CH_PWM | AL | BL)
#define	DRIVE_AC_B								(ACH_PWM | BL)

/*COMMUTATION2 USE*/
#define	DRIVE_A_B								(AH_PWM | BL)
#define	DRIVE_A_C								(AH_PWM | CL)
#define	DRIVE_B_C								(BH_PWM | CL)
#define	DRIVE_B_A								(BH_PWM | AL)
#define	DRIVE_C_A								(CH_PWM | AL)
#define	DRIVE_C_B								(CH_PWM | BL)

/*POSITION CHECK USE*/
#define	PDRIVE_A_BC								(0X3F00 | AH | BL | CL)
#define	PDRIVE_B_CA								(0X3F00 | BH | CL | AL)
#define	PDRIVE_C_AB								(0X3F00 | CH | AL | BL)
#define	PDRIVE_AB_C								(0X3F00 | AH | BH | CL)
#define	PDRIVE_BC_A								(0X3F00 | BH | CH | AL)
#define	PDRIVE_CA_B								(0X3F00 | CH | AH | BL)

#define	PDRIVE_A_B								(0X3F00 |AH | BL)
#define	PDRIVE_A_C								(0X3F00 |AH | CL)
#define	PDRIVE_B_C								(0X3F00 |BH | CL)
#define	PDRIVE_B_A								(0X3F00 |BH | AL)
#define	PDRIVE_C_A								(0X3F00 |CH | AL)
#define	PDRIVE_C_B								(0X3F00 |CH | BL)

/*OTHER USE*/
#define   DRIVER_OFF								0X3F00
#define   DRIVER_ABCL      						(0X3F00 | AL | BL | CL)
#define   DRIVER_ABCH							(0X3F00 | AH | BH | CH)
#define   DRIVER_AL      						                (0X3F00 | AL)
#define   DRIVER_BL      						                (0X3F00 | BL)
#define   DRIVER_CL      						                (0X3F00 | CL)

#endif

#if(PWM_MODE == PWM_MODE_COMPLMT)
#define  AHL_PWM								0X3C00
#define  BHL_PWM								0X3300
#define  CHL_PWM								0X0F00
/*COMMUTATION USE*/
#define	DRIVE_A_BC								(AHL_PWM | BL | CL)
#define	DRIVE_AB_C								(AHL_PWM | CL)
#define	DRIVE_B_CA								(BHL_PWM | CL | AL)
#define	DRIVE_BC_A								(BHL_PWM | AL)
#define	DRIVE_C_AB								(CHL_PWM | AL | BL)
#define	DRIVE_AC_B								(CHL_PWM | BL)

/*COMMUTATION2 USE*/
#define	DRIVE_A_B								(AHL_PWM | BL)
#define	DRIVE_A_C								(AHL_PWM | CL)
#define	DRIVE_B_C								(BHL_PWM | CL)
#define	DRIVE_B_A								(BHL_PWM | AL)
#define	DRIVE_C_A								(CHL_PWM | AL)
#define	DRIVE_C_B								(CHL_PWM | BL)

/*POSITION CHECK USE*/
#define	PDRIVE_A_BC								(0X3F00 | AH | BL | CL)
#define	PDRIVE_B_CA								(0X3F00 | BH | CL | AL)
#define	PDRIVE_C_AB								(0X3F00 | CH | AL | BL)
#define	PDRIVE_AB_C								(0X3F00 | AH | BH | CL)
#define	PDRIVE_BC_A								(0X3F00 | BH | CH | AL)
#define	PDRIVE_CA_B								(0X3F00 | CH | AH | BL)

#define	PDRIVE_A_B								(0X3F00 |AH | BL)
#define	PDRIVE_A_C								(0X3F00 |AH | CL)
#define	PDRIVE_B_C								(0X3F00 |BH | CL)
#define	PDRIVE_B_A								(0X3F00 |BH | AL)
#define	PDRIVE_C_A								(0X3F00 |CH | AL)
#define	PDRIVE_C_B								(0X3F00 |CH | BL)

/*OTHER USE*/
#define  DRIVER_OFF								0X3F00
#define  DRIVER_AL      						                (0X3F00 | AL)
#define  DRIVER_BL      						                (0X3F00 | BL)
#define  DRIVER_CL      						                (0X3F00 | CL)

#define  DRIVER_ABCL      						(0X3F00 | AL | BL | CL)
#define  DRIVER_ABCH							(0X3F00 | AH | BH | CH)
#endif




/*****************************************************************************/
/* Global type definitions ('typedef') */
/*****************************************************************************/
typedef struct
{
	uint8_t u8val1;
	
	volatile u32	VbusAd12;
	
}MOTOR_CONTROL_STR;

extern MOTOR_CONTROL_STR strMcontrol;




/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source) */
/*****************************************************************************/









/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source) */
/*****************************************************************************/
/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Write_Pwm_Duty(unsigned int  uiDutyTemp);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Driver_Off(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/


void MCT_Driver_Hard_Brake(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Driver_Soft_Brake(unsigned int  uiDutyTemp);

void MCT_Driver_Soft_Brake2(uint16_t uiDriver,unsigned int  uiDutyTemp);

/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/
void MCT_Quit_S_Brake(void);




/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/
uint8_t MCT_Check_Signla_Driver(uint8_t idriver);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

uint8_t  MCT_Check_Whole_Driver(void);



/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/
 
void MCT_Driver_Phase(void);

/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/
 void MCT_Driver_PhaseLastPosition(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments	 : 
* Return Value : 
***********************************************************************************************************************/
void MCT_Speed_Set(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments	 : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Set_Pwm_Duty(void);




/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Force_Driver_Phase(void);
void MCT_Driver_Back_Phase(void);



/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/


void MCT_Limit_Avg_Current(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Limit_Peak_Current(void);
/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Limit_Bus_Voltage(void);

/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/
void MCT_Get_Rpm(void);



/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Get_Current_Offset(void);





/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Init_Driver(void);



/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Check_Mos(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Check_Block(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Check_Speed_Low(void);


/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

void MCT_Restart_Brake_Pwm(void);

/***********************************************************************************************************************
* Function Name:
* Description  : 
* Arguments    : 
* Return Value : 
***********************************************************************************************************************/

uint8_t MCT_Charge_Driver_Cap(uint16_t uiDriver,unsigned int  uiDutyTemp);


#endif /* __MOTOR_CONTROL_H__ */
